﻿(function ($) {

    var _hazardSourceService = abp.services.app.hazardSource;
  
    var _$modal = $("#RiskFactorEditModal");
    var _$form = $('form');

    function save() {

        if (!_$form.valid()) {
            return;
        }

        var hazardsource = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js

        //事故类型
        var strlist = "";

        hazardsource.AccidentTypes = [];
        var _$roleCheckboxes = $("input[name='accidentlist']:checked");
        if (_$roleCheckboxes) {
            for (var roleIndex = 0; roleIndex < _$roleCheckboxes.length; roleIndex++) {
                var _$roleCheckbox = $(_$roleCheckboxes[roleIndex]);
                strlist += _$roleCheckbox.val() + " ";
            }
        }


        if ($.trim(strlist) === "") {
            alert("请选择事故类型");
            return;
        }

        hazardsource.AccidentTypes = $.trim(strlist);
        //提交


        abp.ui.setBusy(_$form);
        _hazardSourceService.createOrUpdateHazardSource({ hazardsource }).done(function () {
            _$modal.modal('hide');
            location.reload(true); //reload page to see edited user!
        }).always(function () {
            abp.ui.clearBusy(_$modal);
        });
    }

    
    //保存
    _$form.closest('div.modal-content').find(".save-button").click(function (e) {
        e.preventDefault();       
        save();
    });


    //Handle enter key
    _$form.find('input').on('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            save();
        }
    });

    $.AdminBSB.input.activate(_$form);

    _$modal.on('shown.bs.modal', function () {
        _$form.find('input[type=text]:first').focus();
    });
})(jQuery);